package com.example.service.impl;

import com.example.common.Result;
import com.example.entity.RoleMenu;
import com.example.mapper.RoleMenuMapper;
import com.example.service.PermissionDependencyService;
import com.example.service.RoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 角色菜单服务实现类
 */
@Service
public class RoleMenuServiceImpl implements RoleMenuService {

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Autowired
    private PermissionDependencyService permissionDependencyService;

    @Override
    public Result<List<Long>> getRoleMenuIds(Long roleId) {
        try {
            List<RoleMenu> roleMenus = roleMenuMapper.selectByRoleId(roleId);
            List<Long> menuIds = roleMenus.stream()
                    .map(RoleMenu::getMenuId)
                    .collect(Collectors.toList());
            return Result.success(menuIds);
        } catch (Exception e) {
            return Result.error("查询角色菜单失败：" + e.getMessage());
        }
    }

    @Override
    @Transactional
    public Result<String> assignMenusToRole(Long roleId, List<Long> menuIds) {
        try {
            // 先删除角色的所有菜单
            roleMenuMapper.deleteByRoleId(roleId);

            if (menuIds != null && !menuIds.isEmpty()) {
                // 解析菜单依赖，包含父级菜单
                Set<Long> allMenuIds = permissionDependencyService.resolveMenuDependencies(menuIds);

                // 为每个菜单创建角色菜单关联
                for (Long menuId : allMenuIds) {
                    RoleMenu roleMenu = new RoleMenu();
                    roleMenu.setRoleId(roleId);
                    roleMenu.setMenuId(menuId);
                    roleMenu.setCreateTime(LocalDateTime.now());
                    roleMenu.setDeleted(0);
                    roleMenuMapper.insert(roleMenu);
                }

                String message = "分配菜单成功";
                if (allMenuIds.size() > menuIds.size()) {
                    message += "，已自动包含 " + (allMenuIds.size() - menuIds.size()) + " 个父级菜单";
                }

                return Result.success(message);
            }

            return Result.success("分配菜单成功");
        } catch (Exception e) {
            return Result.error("分配菜单失败：" + e.getMessage());
        }
    }

    @Override
    @Transactional
    public Result<String> removeRoleMenus(Long roleId) {
        try {
            roleMenuMapper.deleteByRoleId(roleId);
            return Result.success("删除角色菜单成功");
        } catch (Exception e) {
            return Result.error("删除角色菜单失败：" + e.getMessage());
        }
    }

    @Override
    @Transactional
    public Result<String> removeMenuRoles(Long menuId) {
        try {
            roleMenuMapper.deleteByMenuId(menuId);
            return Result.success("删除菜单角色关联成功");
        } catch (Exception e) {
            return Result.error("删除菜单角色关联失败：" + e.getMessage());
        }
    }
} 